package xin.lhuo.we.server.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import xin.lhuo.we.server.entity.Node;
import xin.lhuo.we.server.entity.NodeCategory;
import xin.lhuo.we.server.entity.User;
import xin.lhuo.we.server.enums.ResultEnum;
import xin.lhuo.we.server.exception.WeException;
import xin.lhuo.we.server.form.NodeCategoryForm;
import xin.lhuo.we.server.form.NodeForm;
import xin.lhuo.we.server.service.INodeService;
import xin.lhuo.we.server.utils.ResultVoUtil;
import xin.lhuo.we.server.vo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

import static xin.lhuo.we.server.constant.HttpConstant.SESSION_USER;

/**
 * @author Lhuo
 * @date 2019/6/29 21:17
 */
@RestController
@Slf4j
@RequestMapping("/node")
public class NodeController {
    @Autowired
    private INodeService iNodeService;


    @GetMapping("")
    public ResultVo getNode(HttpServletRequest request) {
        List<Node> nodeList = iNodeService.getNode(getUser(request).getId());
        return ResultVoUtil.success(nodeList);
    }

    @PostMapping("")
    public ResultVo createNode(@RequestBody NodeForm nodeForm, HttpServletRequest request) {
        if (StringUtils.isEmpty(nodeForm.getTitle())) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "标题不能为空");
        }
        if (StringUtils.isEmpty(nodeForm.getAuthor())){
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "作者不能为空");
        }

        User user = getUser(request);
        Node node = new Node();
        node.setUserId(user.getId());
        node.setTitle(nodeForm.getTitle());
        node.setCategory(nodeForm.getCategory());
        node.setImageKey(nodeForm.getImageKey());
        node.setAuthor(nodeForm.getAuthor());
        node.setContent(nodeForm.getContent());
        node.setContentHtml(nodeForm.getContentHtml());
        node.setCreateTime(new Date());
        iNodeService.createNode(node);
        return ResultVoUtil.success();
    }

    @GetMapping("/{id}")
    public ResultVo getNodeById(@PathVariable("id") Integer id, HttpServletRequest request) {
        Node node = iNodeService.getNodeById(id);
        return ResultVoUtil.success(node);
    }

    @GetMapping("/category")
    public ResultVo getCategory(HttpServletRequest request) {
        List<NodeCategory> categoryList = iNodeService.getCategory(getUser(request).getId());
        return ResultVoUtil.success(categoryList);
    }

    @PostMapping("/category")
    public ResultVo createCategory(@Valid NodeCategoryForm nodeCategoryForm, BindingResult bindingResult,
                                   HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            throw new WeException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        User user = getUser(request);
        NodeCategory nodeCategory = new NodeCategory();
        nodeCategory.setUserId(user.getId());
        nodeCategory.setName(nodeCategoryForm.getName());
        nodeCategory.setDesc(nodeCategoryForm.getDesc());
        iNodeService.createCategory(nodeCategory);
        return ResultVoUtil.success();
    }

    @GetMapping("/token")
    public ResultVo getQiNiuToken() {
        String token = iNodeService.getQiNiuToken();
        return ResultVoUtil.success(token);
    }


    private User getUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (User) session.getAttribute(SESSION_USER);
    }

}
